//
//  GoodsMessageCell.swift
//  GLUZSwift
//
//  Created by xyanl on 2025/5/8.
//

import UIKit
import Reusable
import SnapKit
import ZIM

extension GoodsMessageCell: Reusable {}

class GoodsMessageCell: MessageCell {
    override class var reuseId: String {
        String(describing: ImageMessageCell.self)
    }
    
    lazy var markView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex: "#DAEBFD")
        containerView.addSubview(view)
        return view
    }()
    
    lazy var iconImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.addRoundedCorners(radius: 8)
        markView.addSubview(imageView)
        return imageView
    }()
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex: "#333333")
        label.font = .systemFont(ofSize: 14)
        markView.addSubview(label)
        return label
    }()
    
    lazy var stateLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex: "#787878")
        label.font = .systemFont(ofSize: 12)
        markView.addSubview(label)
        return label
    }()
    
    lazy var priceLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex: "#FF6A00")
        label.font = .systemFont(ofSize: 14, weight: .semibold)
        markView.addSubview(label)
        return label
    }()
    
    override func setUp() {
        super.setUp()
        
        containerView.addRoundedCorners(radius: 8)
        markView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        iconImageView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(10)
            make.centerY.equalToSuperview()
            make.size.equalTo(CGSize(width: 62, height: 62))
        }
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(iconImageView.snp.right).offset(6)
            make.right.equalToSuperview().offset(-6)
            make.top.equalTo(iconImageView.snp.top)
        }
        
        stateLabel.snp.makeConstraints { make in
            make.left.equalTo(iconImageView.snp.right).offset(6)
            make.right.equalToSuperview().offset(-6)
            make.top.equalTo(titleLabel.snp.bottom).offset(6)
        }
        
        priceLabel.snp.makeConstraints { make in
            make.left.equalTo(iconImageView.snp.right).offset(6)
            make.right.equalToSuperview().offset(-6)
            make.top.equalTo(stateLabel.snp.bottom).offset(6)
        }
        
    }
    
    override func setUpLayout() {
        super.setUpLayout()
        updateImageConstraint()
    }
    
    private func updateImageConstraint() {
        
        guard messageVM is GoodsMessageViewModel else {
            xLog("[GoodsMessageCell]: \(String(describing: messageVM))")
            return
        }
        
        if messageVM?.message.zim?.type == .custom {
            let custom: ZIMCustomMessage = messageVM?.message.zim as! ZIMCustomMessage
            if let dataDic = custom.message.toDictionary() {
                if let model = JsonUtil.modelFromDictionary(dataDic, ShareGoodsToIMModel.self),
                let uRL = URL(string: model.pic) {
                    iconImageView.kf.setImage(with: uRL)
                    titleLabel.text = LanguageManager.isLanguageZH ? model.title: model.title_ru
                    stateLabel.text = model.is_yf == 0 ? "不含运费".localized(.live): "含运费".localized(.live)
                    let text = NSMutableAttributedString(string: model.currency + model.price)
                    text.color(UIColor(hex: "#FF6A00"))
                    text.font(.systemFont(ofSize: 14, weight: .semibold))
                    text.font(.systemFont(ofSize: 14), range: text.string.inRange(of: model.currency).first!)
                    priceLabel.attributedText = text
                }
            }
        }
    }
    
    override func updateContent() {
        super.updateContent()
        guard messageVM is GoodsMessageViewModel else { return }
        updateImageConstraint()
        
    }
    
}
